/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

/**
 *
 * @author Bayu
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.bayu.services.dao.AvgOdcDao;
import com.bayu.services.entity.Avgodc;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/avgodc-update.htm")
public class  AvgOdcUpdate {
private AvgOdcDao dao;

    @Autowired
    public void setDao(AvgOdcDao dao) {
        this.dao = dao;

    }

    @RequestMapping(method=RequestMethod.GET)
    public void displayForm(ModelMap map, String id) {
        List list = this.dao.getById(Long.parseLong(id));
        map.addAttribute("data", list);
    }

    @RequestMapping(method=RequestMethod.POST)
    public String processForm(@ModelAttribute("Avgodc") Avgodc entity,
            BindingResult result, ModelMap map, String date, HttpServletRequest request) {
       
        String id = request.getParameter("id");

         DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            Date tgl;

            //exchangeCurrency.setDate(tgl);
            if(result.hasFieldErrors("date")){
                try {
                    tgl = formarter.parse(date);
                    entity.setDate(tgl);
                } catch (ParseException ex) {
                    Logger.getLogger(GalvaUpdate.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        if (id != null) {
            entity.setId(Long.parseLong(id));
            this.dao.update(entity);
            return "redirect:view-avgodc-data.htm?msg=Data has been updated";
        } else {
            return "redirect:avgodc-update.htm";
        }

    }
}
